/** 
 * @Copyright 中国航信重庆研发中心版权所有（2015） 
 */
package com.sure.minion.web.base;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.sure.minion.web.base.GenericResult;

/**
 * 返回结果国际化处理
 * 
 * @author huangjian
 * @date 2015-5-21
 */
public class GenericResultConverter extends MappingJackson2HttpMessageConverter {
	@Autowired
	private MessageSource messageResource;

	@Override
	protected void writeInternal(Object object, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		if (object instanceof GenericResult) {
			GenericResult genericResult = (GenericResult) object;
			try {
				// 国际化处理
				String i18nMsg = messageResource.getMessage(genericResult.getCode(), null,
						LocaleContextHolder.getLocale());
				genericResult.setMsg(i18nMsg);
			} catch (Exception e) {
				genericResult.setMsg(genericResult.getCode());
			}
		}
		// 执行真正的转换操作
		super.writeInternal(object, outputMessage);
	}
}
